package edu.wtbu.cs.androiddemo;


import static org.junit.Assert.assertEquals;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import edu.wtbu.cs.androiddemo.sqlite.SQLiteHelper;
import edu.wtbu.cs.androiddemo.sqlite.UserInfo;


/**
 * SQLite 测试案例
 *
 */
@RunWith(AndroidJUnit4.class)
public class SQLiteSampleTest {

    public static String logTag = SQLiteSampleTest.class.getName();

    SQLiteHelper sqLiteHelper;
    SQLiteDatabase database;

    @Before
    public void createSQLiteDb() {
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("edu.wtbu.cs.androiddemo", appContext.getPackageName());
        sqLiteHelper = new SQLiteHelper(appContext);
        database = sqLiteHelper.getWritableDatabase();
    }

    @Test
    public void testInsert() {

        String name="chende23";
        String pwd="1234" ;
        String nick="dedeguo";
        ContentValues values = new ContentValues();
        values.put("user_name", name);
        values.put("user_pwd", pwd);
        values.put("user_nick", nick);
        long id = database.insert("userinfo", null, values);
        System.out.println(id);

        values.put("user_name","chende2333");
        long id2 = database.insert("userinfo", null, values);
        System.out.println(id2);
        testQuery();
    }

    @Test
    public void testQueryAll(){
        Cursor cursor = database.query("userinfo",null,null, null,null,null,null,null);
        if (cursor!=null  && cursor.getCount()>0){
            while (cursor.moveToNext()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUser_name(cursor.getString(1));
                userInfo.setUser_pwd(cursor.getString(2));
                userInfo.setUser_nick(cursor.getString(3));
                Log.i(logTag,userInfo.toString());
            }
        }
    }

    @Test
    public void testQuery(){
        Cursor cursor = database.query("userinfo",null,"user_name like ?", new String[]{"chende%"},null,null,null,null);

        if (cursor!=null  && cursor.getCount()>0){
           while (cursor.moveToNext()){
               UserInfo userInfo = new UserInfo();
               userInfo.setUser_name(cursor.getString(1));
               userInfo.setUser_pwd(cursor.getString(2));
               userInfo.setUser_nick(cursor.getString(3));
               System.out.println(userInfo);
           }
       }
    }

    @Test
    public void testDelete(){
        int num = database.delete("userinfo","user_name=?", new String[]{"chende"});
        System.out.println("delete data: "+num);
    }

    @Test
    public void testUpdate(){
        ContentValues values =new ContentValues();
        values.put("user_pwd","pwd");
        values.put("user_nick","nick");
        int num = database.update("userinfo",values,"user_name=?",new String[]{"chende"});

    }

    @After
    public void closeDb() {
        database.close();
    }
}
